package com.example.demojdbcmysql.config;

import com.google.common.collect.Range;
import io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue;
import io.shardingjdbc.core.api.algorithm.sharding.standard.RangeShardingAlgorithm;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.util.CollectionUtils;

/**
 * @Description:
 * @Author: kai.yang
 * @Date: 2019-10-24 14:19
 */
public class UserLogTableRangeShardingAlgorithm implements RangeShardingAlgorithm<Timestamp> {


    private static final String TblUserLogName = "auth_user_log";

    public static final Map<Integer, String> dbConfig = new HashMap<>();

    static {
        dbConfig.put(0, "master1Config." + TblUserLogName+",master2Config."+TblUserLogName);
        dbConfig.put(2019, "master1Config.auth_user_log_201903,master1Config.auth_user_log_201904,master2Config.auth_user_log_201903,master2Config.auth_user_log_201904");
    }

    public static String toUserLogDbConfigString() {
        Collection<String> values = dbConfig.values();
        if (values.size() > 0) {
            StringBuilder stringBuilder = new StringBuilder();
            values.stream().forEach(value -> stringBuilder.append(value).append(","));
            return stringBuilder.substring(0, stringBuilder.length() - 1);
        }
        return TblUserLogName;
    }

    @Override
    public Collection<String> doSharding(Collection<String> collection,
        RangeShardingValue<Timestamp> shardingValue) {
        List<String> result = Arrays.asList(TblUserLogName);
        Range<Timestamp> valueRange = shardingValue.getValueRange();
        Timestamp start = valueRange.lowerEndpoint();
        Timestamp end = valueRange.upperEndpoint();

        shardingTableNames(result, start);
        shardingTableNames(result, end);

        result.retainAll(collection);

        if (CollectionUtils.isEmpty(result)) {
            throw new IllegalArgumentException();
        }

        return result;
    }

    private void shardingTableNames(List<String> result, Timestamp start) {
        String tb_name;
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(start.getTime());
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        if (dbConfig.keySet().contains(year)) {
            if (month >= 1 && month <= 3) {
                tb_name = TblUserLogName + "_" + year + "01";
                result.add(tb_name);
            }
            if (month >= 4 && month <= 6) {
                tb_name = TblUserLogName + "_" + year + "02";
                result.add(tb_name);
            }
            if (month >= 7 && month <= 9) {
                tb_name = TblUserLogName + "_" + year + "03";
                result.add(tb_name);
            }
            if (month >= 10 && month <= 12) {
                tb_name = TblUserLogName + "_" + year + "04";
                result.add(tb_name);
            }
        }
    }


}
